<template>
  <div class="login-box">
    <van-nav-bar title="登录"
                 class="page-nav-bar">
      <van-icon @click="$router.go(-1)"
                slot="left"
                name="cross" />
    </van-nav-bar>

    <!-- 登陆表单 -->
    <van-form ref="form"
              @submit="onSubmit">
      <van-field v-model="form.mobile"
                 name="mobile"
                 placeholder="请输入手机号"
                 :rules="userFormRules.mobile"
                 type="number"
                 maxlength="11">
        <i slot="left-icon"
           class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field v-model="form.code"
                 name="code"
                 placeholder="请输入验证码"
                 :rules="userFormRules.code"
                 type="number"
                 maxlength="6">
        <i slot="left-icon"
           class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down @finish="isCountDownShow=false"
                          v-if="isCountDownShow"
                          :time="60000"
                          format="ss 秒" />
          <van-button v-else
                      @click="onSendCode"
                      native-type="button"
                      class="send-code"
                      round
                      size="small"
                      type="primary">发送验证码</van-button>
        </template>
      </van-field>
      <div>
        <van-button class="login-button"
                    block
                    type="info"
                    native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login, getCode } from '@/api/user.js'
export default {
  name: 'login',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空'
        }, {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '手机号格式错误'
        }],
        code: [{
          required: true,
          message: '验证码不能为空'
        }, {
          pattern: /^\d{6}$/,
          message: '验证码格式错误'
        }]
      },
      isCountDownShow: false
    }
  },

  created () {

  },

  methods: {
    async onSubmit () {
      this.$toast.loading({
        message: '登录中...',
        duration: 0,
        forbidClick: true
      })
      const user = this.form
      try {
        const { data: res } = await login(user)
        this.$store.commit('setUser', res.data)
        this.$toast.success('登录成功')
        this.$router.push('/my')
      } catch (err) {
        if (err.response.status === 400) return this.$toast.fail('手机号或验证码错误')
        this.$toast.fail('网络错误')
      }
    },
    async onSendCode () {
      try {
        await this.$refs.form.validate('mobile')
      } catch (err) {
        return true
      }
      // console.log('验证通过')
      this.isCountDownShow = true
      try {
        await getCode(this.form.mobile)
      } catch (err) {
        if (err.response.status === 429) return this.$toast.fail('操作频繁,请稍后尝试')
        this.$toast.fail('网络错误,请稍后尝试')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.login-box {
  .login-button {
    margin: 53px 28px;
    width: 694px;
    height: 88px;
    background: #6db4fb;
    border-radius: 10px;
    border: none;
  }
  .toutiao {
    font-size: 37px;
  }
  .send-code {
    width: 152px;
    height: 46px;
    background: #ededed;
    color: #666;
    font-size: 22px;
    border: none;
  }
}
</style>
